import {listSubjectData, listSysSymbols} from '@/api/yzt'

const yzt = {
  namespaced: true,
  state: {
    map: undefined,
    mapService: undefined,
    currentSearchLayer: undefined,
    attributeTable: undefined,
    attributeTableData: [],
    symbols: undefined,
    drawControl: undefined,
    showLayerLegend: undefined,
    showLayerStatics: undefined,
    showBufferStatics: undefined,
    bufferStaticsData: undefined,
    bufferAttr: undefined
  },

  mutations: {
    /**
     * 地图
     * @param state
     * @param map
     */
    setMap: (state, map) => {
      state.map = map
    },
    /**
     * 地图服务类
     * @param state
     * @param mapservice
     */
    setMapService: (state, mapservice) => {
      state.mapService = mapservice
    },
    /**
     * 选中当前图层
     * @param state
     * @param currentSearchLayer
     */
    setCurrentSearchLayer: (state, currentSearchLayer) => {
      state.currentSearchLayer = currentSearchLayer
    },
    /**
     * 显示属性表
     * @param state
     * @param isTrue
     */
    setAttributeTable: (state, isTrue) => {
      state.attributeTable = isTrue
    },
    /**
     * 属性表数据
     * @param state
     * @param data
     */
    setAttributeTableData: (state, data) => {
      state.attributeTableData = data
    },
    /**
     * 符号库
     * @param state
     * @param s
     */
    setSymbols: (state, s) => {
      state.symbols = s
    },
    /**
     * 设置DrawControl组件
     * @param state
     * @param draw
     */
    setDrawControl: (state, draw) => {
      state.drawControl = draw
    },
    /**
     * 设置显示图层图例
     * @param state
     * @param isTrue
     */
    setShowLayerLegend: (state, isTrue) => {
      state.showLayerLegend = isTrue
    },
    /**
     * 设置显示图层统计
     * @param state
     * @param isTrue
     */
    setShowLayerStatics: (state, isTrue) => {
      state.showLayerStatics = isTrue
    },
    /**
     * 设置显示缓冲区查询统计
     * @param state
     * @param bool
     */
    setShowBufferStatics: (state, bool) => {
      state.showBufferStatics = bool;
    },
    /**
     * 设置缓冲区统计数据
     * @param state
     * @param data
     */
    setBufferStaticsData: (state, data) => {
      state.bufferStaticsData = data;
    },
    /**
     * 设置缓冲区是否显示并设置对应的缓冲区数据
     * @param state
     * @param data
     */
    setShowAttribute: (state, data) => {
      state.bufferAttr = data;
    }
  },

  actions: {
    /**
     * 图层查询属性
     * @param commit
     * @param state
     * @returns {Promise<any>}
     * @constructor
     */
    QueryLayerAttributes({commit, state}, param) {
      return new Promise((resolve, reject) => {
        state.mapService.queryLayerAttribute(param.layer, param.start, param.size, param.sql ? param.sql : '1=1').then(res => {
          const features = res.result.recordsets[0].features.features
          commit('setAttributeTableData', features)
          resolve({data: features, total: res.result.totalCount})
        }).catch(e => {
          reject(e)
        })
      })
    },
    /**
     * 请求系统符号库
     * @param commit
     * @param state
     * @param param
     * @returns {Promise<any>}
     * @constructor
     */
    RequestSysSymbols({commit, state}, param) {
      return new Promise((resolve, reject) => {
        listSysSymbols().then(res => {
          commit('setSymbols', res.data)
          resolve(res.data)
        }).catch(e => {
          reject(e)
        })
      })
    }
  }

}

export default yzt
